package com.xdjd.mobile.business.util;


import org.apache.commons.codec.binary.Base64;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.Random;

public class ValidateCodeUtil {

        private static Validate validate = null;                  //验证码类，用于最后返回此对象，包含验证码图片base64和真值
        //将构造函数私有化 禁止new创建
        private ValidateCodeUtil() {
            super();
        }

        /**
         * 获得颜色
         */
        private static Color getRandColor(int fc, int bc) {
            Random random = new Random();
            if(fc>255) fc=255;
            if(bc>255) bc=255;
            int r=fc+random.nextInt(bc-fc);
            int g=fc+random.nextInt(bc-fc);
            int b=fc+random.nextInt(bc-fc);
            return new Color(r,g,b);
        }

        /**
         *  生成Base64图片验证码
         */
        public static Validate getRandomCode() {
            validate = validate==null?new Validate():validate;
            // 在内存中创建图象
            int width=96, height=36;
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            Graphics g = image.getGraphics();
            //生成随机类
            Random random = new Random();
            g.setColor(getRandColor(243,247));
            g.fillRect(0, 0, width, height);
            g.setFont(new Font("zh_CN"/*"Times New Roman"*/,Font.PLAIN,36));

            // 随机产生155条干扰线，使图象中的认证码不易被其它程序探测到
            g.setColor(getRandColor(160,200));
            for (int i=0;i<155;i++)
            {
                int x = random.nextInt(width);
                int y = random.nextInt(height);
                int xl = random.nextInt(12);
                int yl = random.nextInt(12);
                g.drawLine(x,y,x+xl,y+yl);
            }

            // 取随机产生的认证码(4位数字/字母组合)
            String randString = "0123456789";
            String sRand="";
            for (int i=0;i<4;i++){
                String rand=String.valueOf(randString.charAt(random.nextInt(randString.length())));
                sRand+=rand;
                // 将认证码显示到图象中
                g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),
                        20+random.nextInt(110)));//调用函数出来的颜色相同，可能是因为种子太接近，所以只能直接生成
                g.drawString(rand,16*i+15,30);
            }
            validate.setValue(sRand);
            g.dispose();

            ByteArrayOutputStream bs = null;
            try {
                bs = new ByteArrayOutputStream();
                ImageIO.write(image, "png", bs);//将绘制得图片输出到流
                String imgsrc = Base64.encodeBase64String(bs.toByteArray());
                validate.setBase64Str(imgsrc);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    bs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally{
                    bs = null;
                }
            }
            return validate;
        }

        /**
         * @Description: 验证码类
         */
        public static class Validate implements Serializable {
            private static final long serialVersionUID = 1L;
            private String Base64Str;       //Base64 值
            private String value;           //验证码值

            public String getBase64Str() {
                return Base64Str;
            }
            public void setBase64Str(String base64Str) {
                Base64Str = base64Str;
            }
            public String getValue() {
                return value;
            }
            public void setValue(String value) {
                this.value = value;
            }
        }

}
